$(function () {
    //判断是否是管理员
    $.ajax({
        url:"easybuy/admin/jurisdiction",
        success:function (data) {
            if (data!="success"){
                alert("你没有权限访问！");
                location.href="Index.html";
            }
        }
    });

    $("#tou").load("tou.html");

    //显示新增新闻页面
    $("#addNews").click(function () {
        $("#app").empty();
        $("#app").append("<p></p>\n" +
            "\n" +
            "            <div class=\"mem_tit\" style=\"text-align: center\">\n" +
            "                新增新闻\n" +
            "            </div>\n" +
            "            <table border=\"0\" class=\"mem_tab\" style=\"width:930px; margin-bottom:30px;\" cellspacing=\"0\" cellpadding=\"0\" id=\"news\">\n" +
            "                <tr>\n" +
            "                    <td width=\"150\" class=\"tx_l\">标题</td>\n" +
            "                    <td width=\"680\">\n" +
            "                        <input type=\"text\" value=\"\" class=\"tx_ipt\" name=\"title\"/>\n" +
            "                        <font style=\"color: red\">(*标题必须唯一)</font>\n" +
            "                    </td>\n" +
            "                </tr>\n" +
            "                <tr valign=\"top\">\n" +
            "                  <td class=\"tx_l\">内容</td>\n" +
            "                  <td><textarea class=\"tx_txt\" name=\"content\"></textarea></td>\n" +
            "                </tr>\n" +
            "                <tr height=\"70\">\n" +
            "                  <td colspan=\"2\" align=\"center\">\n" +
            "                      <input type=\"button\" value=\"增加\" class=\"btn_tj\" id=\"addNews\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n" +
            "                      <input type=\"button\" value=\"返回\" class=\"btn_tj\" id=\"back\"/>\n" +
            "                  </td>\n" +
            "                </tr>\n" +
            "            </table>");

        //返回
        $("#back").click(function () {
            // window.history.go(-1);
            location.href="adminNews.html";
        });

        //判断标题是否唯一
        $("[name='title']").change(function () {
            var title = $("[name='title']").val();
            $.ajax({
                url: "easybuy/isTitle",
                data: {
                    title: title
                },
                success: function (data) {
                    if (data=="failed"){
                        alert("标题重复，请重新填写！");
                        $("[name='title']").val("");
                    }
                }
            });
        });

        addNews();
    })
});
//新增新闻
function addNews() {
    $("#addNews").click(function () {
        var title = $("[name='title']").val();
        var content = $("[name='content']").val();
        $.ajax({
            url: "easybuy/addNews",
            data: {
                title: title,
                content: content
            },
            success: function (data) {
                if (data == "success") {
                    alert("增加成功！");
                    location.href = "adminNews.html";
                } else {
                    alert("增加失败,请联系管理员！");
                }
            }
        })
    });
}
